package com.dc.base.view.dialog

import android.util.SparseArray
import android.view.View
import android.widget.TextView
import androidx.annotation.IdRes
import java.lang.ref.WeakReference

class DialogViewHolder(private val convertView: View) {
    private val mViews: SparseArray<WeakReference<View>> = SparseArray()

    companion object {
        fun createView(view: View): DialogViewHolder {
            return DialogViewHolder(view)
        }
    }

    public fun setText(@IdRes viewId: Int, text: String) {
        val tv = getView<TextView>(viewId)
    }

    public fun <T: View> getView(@IdRes viewId: Int) : T {
        val viewReference = mViews.get(viewId)
        var view = viewReference?.get()
        if (view == null) {
            view = convertView.findViewById(viewId)
            mViews.put(viewId, WeakReference(view))
        }
        return view as T
    }

    public fun setOnClickListener(@IdRes viewId: Int, listener: View.OnClickListener) {
        val view = getView<View>(viewId)
        view.setOnClickListener(listener)
    }
}